﻿using PhotovoltaicETL;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace ConsoleForTest
{
    class Program
    {
        /// <summary>
        /// 每次计时对于整时偏移毫秒数，用于错开误差或其他整点计算时导致的数据不一致或错误
        /// </summary>
        const int startAddMillisecond = 100;
        static DataETL contex = null;
        static System.Timers.Timer tm = new System.Timers.Timer();
        static void Main(string[] args)
        {
            contex = DataETL.Context;

            tm.Elapsed += Tm_Elapsed;

            tm.Interval = 60000 + startAddMillisecond - DateTime.Now.Second * 1000 - DateTime.Now.Millisecond;

            //Tm_Elapsed(null, null);
            tm.Start();
            string input;
            do
            {
                input = Console.ReadLine();
            }
            while (input.Equals("exit", StringComparison.CurrentCultureIgnoreCase)
            || input.Equals("quit", StringComparison.CurrentCultureIgnoreCase));
        }
        /// <summary>
        /// 1分钟定时器定时处理任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            tm.Interval = 60000 / 10- DateTime.Now.Millisecond + startAddMillisecond;
            try
            {
                Console.WriteLine("Work in " + DateTime.Now.ToString("HH-mm-ss:fff"));
                //Debug.WriteLine(DateTime.Now.ToString("HH-mm-ss:fff"));
                contex.DoETL();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
    }
}
